<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActionController::ParamsWrapper</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActionController::ParamsWrapper 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/actionpack/lib/action_controller/metal/params_wrapper_rb.html">actionpack/lib/action_controller/metal/params_wrapper.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Wraps the parameters hash into a nested hash. This will allow clients to
submit POST requests without having to specify any root elements.</p>

<p>This functionality is enabled in
<code>config/initializers/wrap_parameters.rb</code> and can be customized.
If you are upgrading to Rails 3.1, this file will need to be created for
the functionality to be enabled.</p>

<p>You could also turn it on per controller by setting the format array to a
non-empty array:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">UsersController</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ApplicationController</span>
  <span class="ruby-identifier">wrap_parameters</span> <span class="ruby-identifier">format</span><span class="ruby-operator">:</span> [:<span class="ruby-identifier">json</span>, :<span class="ruby-identifier">xml</span>]
<span class="ruby-keyword">end</span>
</pre>

<p>If you enable <code>ParamsWrapper</code> for <code>:json</code> format,
instead of having to send JSON parameters like this:</p>

<pre>{&quot;user&quot;: {&quot;name&quot;: &quot;Konata&quot;}}</pre>

<p>You can send parameters like this:</p>

<pre>{&quot;name&quot;: &quot;Konata&quot;}</pre>

<p>And it will be wrapped into a nested hash with the key name matching the
controller’s name. For example, if you’re posting to
<code>UsersController</code>, your new <code>params</code> hash will look
like this:</p>

<pre class="ruby">{<span class="ruby-string">&quot;name&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;Konata&quot;</span>, <span class="ruby-string">&quot;user&quot;</span> =<span class="ruby-operator">&gt;</span> {<span class="ruby-string">&quot;name&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;Konata&quot;</span>}}
</pre>

<p>You can also specify the key in which the parameters should be wrapped to,
and also the list of attributes it should wrap by using either
<code>:include</code> or <code>:exclude</code> options like this:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">UsersController</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ApplicationController</span>
  <span class="ruby-identifier">wrap_parameters</span> :<span class="ruby-identifier">person</span>, <span class="ruby-identifier">include</span><span class="ruby-operator">:</span> [:<span class="ruby-identifier">username</span>, :<span class="ruby-identifier">password</span>]
<span class="ruby-keyword">end</span>
</pre>

<p>On <a href="../ActiveRecord.html">ActiveRecord</a> models with no
<code>:include</code> or <code>:exclude</code> option set, it will only
wrap the parameters returned by the class method
<code>attribute_names</code>.</p>

<p>If you’re going to pass the parameters to an <code>ActiveModel</code>
object (such as <code>User.new(params[:user])</code>), you might consider
passing the model class to the method instead. The
<code>ParamsWrapper</code> will actually try to determine the list of
attribute names from the model and only wrap those attributes:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">UsersController</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ApplicationController</span>
  <span class="ruby-identifier">wrap_parameters</span> <span class="ruby-constant">Person</span>
<span class="ruby-keyword">end</span>
</pre>

<p>You still could pass <code>:include</code> and <code>:exclude</code> to set
the list of attributes you want to wrap.</p>

<p>By default, if you don’t specify the key in which the parameters would be
wrapped to, <code>ParamsWrapper</code> will actually try to determine if
there’s a model related to it or not. This controller, for example:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Admin</span><span class="ruby-operator">::</span><span class="ruby-constant">UsersController</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ApplicationController</span>
<span class="ruby-keyword">end</span>
</pre>

<p>will try to check if <code>Admin::User</code> or <code>User</code> model
exists, and use it to determine the wrapper key respectively. If both
models don’t exist, it will then fallback to use <code>user</code> as the
key.</p>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">MODULE</span>
          <a href="ParamsWrapper/ClassMethods.html">ActionController::ParamsWrapper::ClassMethods</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>P</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ParamsWrapper.html#method-i-process_action">process_action</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">EXCLUDE_PARAMETERS</td>
            <td>=</td>
            <td class="attr-value">%w(authenticity_token _method utf8)</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-process_action">
            
              <b>process_action</b>(*args)
            
            <a href="ParamsWrapper.html#method-i-process_action" name="method-i-process_action" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Performs parameters wrapping upon the request. Will be called automatically
by the metal call stack.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-process_action_source')" id="l_method-i-process_action_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/80b8df5f3d09205d8f239e9aefd5ed0e0ddfd4a5/actionpack/lib/action_controller/metal/params_wrapper.rb#L232" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-process_action_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_controller/metal/params_wrapper.rb, line 232</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">process_action</span>(*<span class="ruby-identifier">args</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">_wrapper_enabled?</span>
    <span class="ruby-identifier">wrapped_hash</span> = <span class="ruby-identifier">_wrap_parameters</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">request_parameters</span>
    <span class="ruby-identifier">wrapped_keys</span> = <span class="ruby-identifier">request</span>.<span class="ruby-identifier">request_parameters</span>.<span class="ruby-identifier">keys</span>
    <span class="ruby-identifier">wrapped_filtered_hash</span> = <span class="ruby-identifier">_wrap_parameters</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">filtered_parameters</span>.<span class="ruby-identifier">slice</span>(*<span class="ruby-identifier">wrapped_keys</span>)

    <span class="ruby-comment"># This will make the wrapped hash accessible from controller and view</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">parameters</span>.<span class="ruby-identifier">merge!</span> <span class="ruby-identifier">wrapped_hash</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">request_parameters</span>.<span class="ruby-identifier">merge!</span> <span class="ruby-identifier">wrapped_hash</span>

    <span class="ruby-comment"># This will make the wrapped hash displayed in the log file</span>
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">filtered_parameters</span>.<span class="ruby-identifier">merge!</span> <span class="ruby-identifier">wrapped_filtered_hash</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">super</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    